package org.noka.mservice.base;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.Serializable;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 * *******************************************************
 * <p>@Description httpServletrequest封装</p>
 * <p>@Version 1.0.0</p>
 * <p>@author rebin</p>
 * <p>@time 2017年5月5日 下午12:55:44</p>
 * <p>xiefangjian@163.com</p>
 * <p>Copyright © Noka Laboratory.All Rights Reserved.</p>
 * *****************************************************
 */
public class NokaServletRequest  extends HashMap<String,Object> implements Serializable{
	private static final long serialVersionUID = -4088912385569849565L;
	private static final String ParameterMap = "ParameterMap";
	private static final String ParameterNames = "ParameterNames";
	private static final String PathInfo = "PathInfo";
	private static final String AttributeNames ="AttributeNames";
	private static final String InputStream ="InputStream";
	private static final String session ="session";
	
	public NokaServletRequest(){}
	public NokaServletRequest(Map<String,Object> m){
		super(m);
	}
	public NokaServletRequest(String key, Object value){
		put(key, value);
	}
	//=====================================================================
	public String getParameter(String name){
		if(get(name) instanceof String){
			return (String)get(name);
		}
		return null;
	}
	public Map<?, ?> getParameterMap(){
		if(get(ParameterMap) instanceof Map){
			return (Map<?, ?>)get(ParameterMap);
		}
		return null;
	}
	public Enumeration<?> getParameterNames(){
		if(get(ParameterNames) instanceof Enumeration){
			return (Enumeration<?>)get(ParameterNames);
		}
		return null;
	}
	public String[] getParameterValues(String name){
		if(get(name) instanceof String[]){
			return (String[])get(name);
		}else if(get(name) instanceof String){
			return new String[]{(String)get(name)};
		}
		return null;
	}
	public String getPathInfo(){
		if(get(PathInfo) instanceof String){
			return (String)get(PathInfo);
		}
		return null;
	}
	public Object getAttribute(String name){
		return get(name);
	}
	public Enumeration<?> getAttributeNames(){
		if(get(AttributeNames) instanceof Enumeration){
			return (Enumeration<?>)get(AttributeNames);
		}
		return null;
	}
	public InputStream getInputStream(){
		if(get(InputStream) instanceof String){
			try{
				BASE64Decoder base64 = new BASE64Decoder();
				return new ByteArrayInputStream(base64.decodeBuffer((String)get(InputStream)));
			}catch(Exception se){}
		}
		return null;
	}
	public NokaServletSession getSession(){
		if(get(session) instanceof NokaServletSession){
			return (NokaServletSession)get(session);
		}
		return null;
	}
	//======================================================================
	public void setParameter(String name,Object value){
		put(name, value);
	}
	public void setParameterMap(Map<?,?> map){
		put(ParameterMap,map);
	}
	public void  setParameterNames( Enumeration<?> value){
		put(ParameterNames, value);
	}
	public void setParameterValues(String name,String[] value){
		put(name, value);
	}
	public void setPathInfo(String value){
		put(PathInfo, value);
	}
	public void setAttribute(String name,Object value){
		put(name, value);
	}
	public void setAttributeNames(Enumeration<?> value){
		put(AttributeNames, value);
	}
	public void setInputStream(InputStream value){
		if(null!=value){
			try{
				BASE64Encoder base64 = new BASE64Encoder();
				ByteArrayOutputStream out=new ByteArrayOutputStream();
				base64.encode(value, out);
				out.close();
				value.close();
				put(InputStream, new String(out.toByteArray()));
			}catch(Exception e){}
		}
	}
	public void setSession(NokaServletSession nsession){
		put(session,nsession);
	}
}
